package com.jeasonzhao.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jeasonzhao.commons.utils.Algorithms;

public abstract class AbstractServlet extends HttpServlet
{
    private static final long serialVersionUID = 1L;
    public static final String DEFAULT_ENCODING="GBK";
    private String encoding="GBK";

    public AbstractServlet()
    {
        super();
    }

    public void doGet(HttpServletRequest request,HttpServletResponse response)
        throws ServletException,IOException
    {
        internal_handler(request,response);
    }

    public void doPost(HttpServletRequest request,HttpServletResponse response)
        throws ServletException,IOException
    {
        internal_handler(request,response);
    }

    private void internal_handler(HttpServletRequest request,HttpServletResponse response)
        throws IOException,ServletException
    {
        if(null!=this.encoding && this.encoding.trim().length()>0)
        {
            response.setContentType("text/html; charset="+this.encoding.trim());
        }
        execute(request,response);
    }

    public abstract void execute(HttpServletRequest request,HttpServletResponse response)
        throws ServletException,IOException;

    public String getRequestFilePart(HttpServletRequest request,String fileExtension)
    {
        if(null == request)
        {
            return null;
        }
        else
        {
            String strUrl = request.getRequestURI();
            if(null != strUrl)
            {
                int n = strUrl.lastIndexOf('/');
                if(n >= 0)
                {
                    strUrl = strUrl.substring(n + 1);
                }
                n = strUrl.lastIndexOf('\\');
                if(n >= 0)
                {
                    strUrl = strUrl.substring(n + 1);
                }
                if(null != fileExtension && fileExtension.trim().length() < 0)
                {
                    fileExtension = fileExtension.trim().toLowerCase();
                    if(fileExtension.startsWith(".") == false)
                    {
                        fileExtension = "." + fileExtension;
                    }
                    n = strUrl.toLowerCase().lastIndexOf(fileExtension);
                    if(n > 0)
                    {
                        strUrl = strUrl.substring(0,n);
                    }
                }
            }
            return strUrl;
        }
    }

    protected final void sandbox(final HttpServletRequest request)
    {
        if(null == request)
        {
            return;
        }
        System.out.println("--------------------------Servlet Request-----------------------------------");
        System.out.println(">> " + request.getRequestURL());
        
            java.util.Map<?,?> map = request.getParameterMap();
            for(java.util.Map.Entry<?,?> m : map.entrySet())
            {
                System.out.println(">>PARAM:" + Algorithms.toString(m.getKey()) + " = " + Algorithms.toString(m.getValue()));
            }
        System.out.println("---------------------------------------------------------------------------");
    }
    public String getEncoding()
    {
        return this.encoding;
    }
    public void setEncoding(String coding)
    {
        this.encoding=coding;
    }
}
